package com.hyg.module_third_party_demo.activity;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hyg.lib_common.Base.BaseActivity;
import com.hyg.lib_common.constant.Constants;
import com.hyg.module_third_party_demo.R;
import com.jaeger.library.StatusBarUtil;

import java.util.Random;

/**
 * @author visitant
 * @description 状态栏设置
 * @time 2020/11/13 16:42
 */
@Route(path = Constants.PATH_ACTIVITY_MODULE_DEMO_STATUSBAR)
public class StatusbarActivity extends BaseActivity {
    private DrawerLayout mDrawerLayout;
    private Toolbar mToolbar;
    private CheckBox mChbTranslucent;
    private Button mBtnSetColor;
    private Button mBtnUseInFragment;

    private ViewGroup contentLayout;
    private SeekBar mSbChangeAlpha;
    private TextView mTvStatusAlpha;

    private int mStatusBarColor;
    private int mAlpha = StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA;

    int currentColor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statusbar);
        init();
    }


    public void init() {
        mDrawerLayout = findViewById(R.id.drawer_layout);
        contentLayout = findViewById(R.id.main);
        mToolbar = findViewById(R.id.toolbar);
        mChbTranslucent = findViewById(R.id.chb_translucent);
        mBtnSetColor = findViewById(R.id.btn_set_color);
        mBtnUseInFragment = findViewById(R.id.btn_use_in_fragment);
        mSbChangeAlpha = findViewById(R.id.sb_change_alpha);
        mTvStatusAlpha = findViewById(R.id.tv_status_alpha);

        setSupportActionBar(mToolbar);                      //初始化toobar
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open,
                R.string.navigation_drawer_close);                  //初始化DrawerLayout
        mDrawerLayout.setDrawerListener(toggle);
        toggle.syncState();

        initButton();
        initSeekBar();
        setStatusBar();
    }


    /**
     * @description 初始化按钮监听
     * @author visitant
     * @time 2020/11/17 9:34
     */
    public void initButton() {
        mBtnSetColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mChbTranslucent.isChecked()) {
                    Random random = new Random();
                    mStatusBarColor = 0xff000000 | random.nextInt(0xffffff);
                    mToolbar.setBackgroundColor(mStatusBarColor);
                    StatusBarUtil.setColor(StatusbarActivity.this, mStatusBarColor, mAlpha);
                }
            }
        });

        mBtnUseInFragment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToIntent(Constants.PATH_ACTIVITY_MODULE_DEMO_STATUSBAR_FRAGMENT, false);
            }
        });

        mChbTranslucent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mChbTranslucent.isChecked()) {
                    contentLayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_monkey));
                    StatusBarUtil.setTranslucentForDrawerLayout(StatusbarActivity.this, mDrawerLayout, mAlpha);
                    mToolbar.setBackgroundColor(getResources().getColor(android.R.color.transparent));
                } else {
                    contentLayout.setBackgroundDrawable(null);
                    mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                    StatusBarUtil.setColorForDrawerLayout(StatusbarActivity.this, mDrawerLayout,
                            getResources().getColor(R.color.colorPrimary), mAlpha);
                    mStatusBarColor = getResources().getColor(R.color.colorPrimary);
                }
            }
        });
    }


    /**
     * @description 初始化seekbar 监听
     * @author visitant
     * @time 2020/11/17 9:34
     */
    public void initSeekBar() {
        mSbChangeAlpha.setMax(255);
        mSbChangeAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mAlpha = progress;
                if (mChbTranslucent.isChecked()) {
                    StatusBarUtil.setTranslucentForDrawerLayout(StatusbarActivity.this, mDrawerLayout, mAlpha);
                } else {
                    StatusBarUtil.setColorForDrawerLayout(StatusbarActivity.this, mDrawerLayout, mStatusBarColor, mAlpha);
                }
                mTvStatusAlpha.setText(String.valueOf(mAlpha));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mSbChangeAlpha.setProgress(StatusBarUtil.DEFAULT_STATUS_BAR_ALPHA);
    }

    /**
     * @description 设置StatusBar颜色
     * @author visitant
     * @time 2020/11/17 9:35
     */
    protected void setStatusBar() {
        mStatusBarColor = getResources().getColor(R.color.colorPrimary);
        StatusBarUtil.setColorForDrawerLayout(this, (DrawerLayout) findViewById(R.id.drawer_layout), mStatusBarColor, mAlpha);
    }
}